package com.example.fanday.play.mvp.base;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.fanday.play.base.BaseApplication;
import com.example.fanday.play.di.component.DaggerFragmentComponent;
import com.example.fanday.play.di.component.FragmentComponent;
import com.example.fanday.play.di.module.FragmentModule;

/**
 * Created by fanday on 2017/8/13.
 */

public abstract class BaseFragment<T extends BasePresenter> extends Fragment implements BaseView{
    protected T presenter;
    protected FragmentComponent fragmentComponent;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        initFragmentComponent();
        presenter = initInject();
        presenter.attachView(this);
        return View.inflate(fragmentComponent.getFragmentContext(),getLayoutRes(),null);
    }

    protected abstract T initInject() ;

    protected abstract  @LayoutRes int getLayoutRes() ;

    private void initFragmentComponent() {
        fragmentComponent = DaggerFragmentComponent.builder().
                applicationComponent(((BaseApplication) getActivity().getApplication()).
                        getApplicationComponent()).fragmentModule(new FragmentModule(this))
                .build();

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }
}
